# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=wt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.10.4
pkgrel=3
pkgdesc="A C++ library and application server for developing and deploying web applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://www.webtoolkit.eu/"
msys2_repository_url="https://github.com/emweb/wt"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-pygments"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql: for PostgreSQL Dbo support"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: for MySQL Dbo support"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: for Sqlite Dbo support")
source=("${msys2_repository_url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
         '001-wt-4.4.0-fix-find-gm.patch'
         '002-wt-4.4.0-skip-some-examples.patch'
         '003-wt-4.4.0-disable-win32-configdir.patch'
         '004-wt-4.4.0-disable-win32-examples-bat.patch'
         '005-fix-build-against-boost-1.85.patch')
sha256sums=('dd6092abe599d9254242225e801da38085847b3147c1748e1212f274398a2e9a'
            '8e42ec247e796117363d8cbfad7b36ba2e4387597f431adbc8846e4ae78d6610'
            '0ff81e1ce798e0fb07ed37b099d91b120adb5e517ebaddf63892ecf991eeb8e3'
            '746aaec5c34b1725514111a29376d5484cf7c9e23cc3d2f16e737f32c67211c6'
            'e0bcd9a9b5bff0e3b13cc49dc029d35ec36a42e61c0449ddee97b21d2c4e39a6'
            '72e48debf256b3b53b48881bc0551e1ccf62d390b268f932566d42c75896daf1')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-wt-4.4.0-fix-find-gm.patch \
    002-wt-4.4.0-skip-some-examples.patch \
    003-wt-4.4.0-disable-win32-configdir.patch \
    004-wt-4.4.0-disable-win32-examples-bat.patch \
    005-fix-build-against-boost-1.85.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DCONNECTOR_HTTP=ON \
    -DWT_WRASTERIMAGE_IMPLEMENTATION=GraphicsMagick \
    -DGM_PREFIX=${MINGW_PREFIX} \
    -DWEBUSER=http \
    -DWEBGROUP=http \
    -DUSE_SYSTEM_SQLITE3=ON \
    -DBUILD_EXAMPLES=OFF \
    -DINSTALL_EXAMPLES=ON \
    -DINSTALL_DOCUMENTATION=ON \
    -DBUILD_TESTS=OFF \
    -DSHARED_LIBS=ON \
    -DBOOST_DYNAMIC=ON \
    -DHARU_DYNAMIC=ON \
    -DMYSQL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/wt/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
